package com.rw.coco.listener;

import com.rw.coco.api.bot.CocoApi;
import com.rw.coco.event.KookGroupMessageEvent;
import snw.jkook.event.EventHandler;
import snw.jkook.event.Listener;
import snw.jkook.event.channel.ChannelMessageEvent;
import snw.jkook.message.TextChannelMessage;
import snw.jkook.message.component.BaseComponent;
import snw.jkook.message.component.MarkdownComponent;

/**
 * 群组消息监听器
 */
public class CocoGroupListener implements Listener {
    private final CocoApi coco;

    public CocoGroupListener(CocoApi coco) {
        this.coco = coco;
    }

    @EventHandler
    public void onEvent(ChannelMessageEvent event) {
        // 如果是机器人，则忽略事件
        if (event.getMessage().getSender().isBot()) {
            return;
        }

        // 获取消息
        TextChannelMessage eventMessage = event.getMessage();
        BaseComponent component = eventMessage.getComponent();

        // 如果是文字消息
        if (component instanceof MarkdownComponent) {
            KookGroupMessageEvent messageEvent = new KookGroupMessageEvent(eventMessage);
            coco.getMessageHandler().handle(messageEvent);
        }
    }
}
